import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import router from '../router/index'
// import { stat } from 'fs'
Vue.use(Vuex)

export default new Vuex.Store({
  // 全局共享的数据放在这里
  state: {
    hasLogin: false,
    loginID: '',
    userId: 0,
    count: 0,
    // 首页热门推荐
    hotTuiJian: [],
    altumnList: [],
    // 榜单数据
    soaringList: [],
    newringList: [],
    ownringList: [],
    bangid: 19723756,
    // 排行榜详情
    detaillist: {},
    songslist: [],
    // 排行榜数据
    toplist: [],
    // 通知-@
    atInfo: []
  },
  // 用于变更store中的数据（不能在组件中直接修改×）虽然麻烦，但是可以集中监控数据变化
  // !!只有mutations才有资格修改state数据
  mutations: {
    setHasLogin(state, val) {
      state.hasLogin = val
    },
    setLoginID(state, val) {
      // console.log('设置的ID：', val)
      state.loginID = val
    },
    // 首页热门推荐
    getHotTui(state, list) {
      state.hotTuiJian = list
    },
    getAltumns(state, list) {
      state.altumnList = list
    },
    // 点击榜单名，跳至榜单
    goBangDan(state, id) {
      router.push({ path: '/discover/toplist', query: { id: id } })
    },
    // 设置榜单数据
    getBangDanData(state, [list, index]) {
      if (index == 1) {
        state.soaringList = list
      } else if (index == 2) {
        state.newringList = list
      } else if (index == 3) {
        state.ownringList = list
      }
    },
    // 点击榜单中的歌曲名，跳转至歌曲页
    goSong(state, id) {
      router.push({ path: '/song', query: { id: id } })
    },
    getATInfo(state, val) {
      state.atInfo = val
    }
  },
  // action执行异步操作，但是不能直接修改state，只能通过触发mutations中方法修改
  actions: {
    // 首页热门推荐
    async getHotTuiJian(context) {
      let res = await axios.get('https://autumnfish.cn//top/playlist?limit=8&order=hot')
      context.commit('getHotTui', res.data.playlists)
    },
    // 首页新碟
    async getAltumnList(context) {
      let res = await axios.get('https://autumnfish.cn//album/list')
      context.commit('getAltumns', res.data.products)
      // this.altumnList = res.data.products
      // console.log(res)
      // let res2 = await axios.get('https://autumnfish.cn//top/album?offset=' + noeoff + '&limit=5');
      // console.log('pppppppppppppppp')
      // console.log(res2)
      // this.altumnList = res.data.weekData
    },
    // 获取三个榜单的数据
    async getSoaringList(context) {
      let res = await axios.get('https://autumnfish.cn//playlist/detail?id=19723756')
      context.commit('getBangDanData', [res.data.playlist.tracks.slice(0, 10), 1])

      let reser = await axios.get('https://autumnfish.cn//playlist/detail?id=3779629')
      context.commit('getBangDanData', [reser.data.playlist.tracks.slice(0, 10), 2])

      let resth = await axios.get('https://autumnfish.cn//playlist/detail?id=2884035')
      context.commit('getBangDanData', [resth.data.playlist.tracks.slice(0, 10), 3])
    },
    async getAT(context) {
      console.log('艾特信息：')
      let res = await axios.get('https://autumnfish.cn//msg/forwards?limit=30')
      console.log(res)
      context.commit('getATInfo', res.data)
    }
  },
  // getter
  getters: {
    showCount(state) {
      return 'count的值为：' + state.count
    }
  },
  modules: {}
})
